package cn.ekuma.epos.datalogic.businesslogic.orderprocess.impl;

import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.OrderLine;
import com.openbravo.bean.erp.OrderType;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerWriteBasic;

import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.businesslogic.orderprocess.AbstractOrderProcess;

public class PriceOrderProcess extends AbstractOrderProcess {

	@Override
	public void saveOrder(I_DataLogicERP dlERP, Order order)
			throws BasicException {
        final OrderType oType = order.getDefineType();
		
        if(oType.getPriceType() == OrderType.PRICETYPE_SellPrice||oType.getPriceType() == OrderType.PRICETYPE_SellPrice||oType.getPriceType() == OrderType.PRICETYPE_CustomerPrice||oType.getPriceType() == OrderType.PRICETYPE_WholesalePrice)
		for (OrderLine l : order.getLines()) {
		if (oType.isChangePrice() && l.getProductID() != null) {
            String priceColumn = "PRICEBUY";
            if (oType.getPriceType() == OrderType.PRICETYPE_SellPrice) {
                priceColumn = "PRICESELL";
            } else if (oType.getPriceType() == OrderType.PRICETYPE_CustomerPrice) {
                priceColumn = "CUSTOMERPRICESELL";
            } else if (oType.getPriceType() == OrderType.PRICETYPE_WholesalePrice) {
                priceColumn = "WHOLESALEPRICE";
            }
            new PreparedSentence(dlERP.getSession(), "UPDATE PRODUCTS SET " + priceColumn + " = ? WHERE ID = ? ", new SerializerWriteBasic(Datas.DOUBLE, Datas.STRING)).exec(l.getPrice(), l.getProductID());
          }
		}
	}


	@Override
	public String getName() {
		return "PriceOrderProcess";
	}


}
